package cn.ys.org.service.impl;

import cn.ys.base.enm.ShopEnum;
import cn.ys.base.util.*;
import cn.ys.org.domain.Employee;
import cn.ys.org.domain.Shop;
import cn.ys.org.domain.ShopAuditLog;
import cn.ys.org.dto.ShopAuditLogDto;
import cn.ys.org.mapper.EmployeeMapper;
import cn.ys.org.mapper.ShopAuditLogMapper;
import cn.ys.org.mapper.ShopMapper;
import cn.ys.org.service.IShopService;
import cn.ys.base.service.impl.BaseServiceImpl;
import cn.ys.system.domain.Role;
import cn.ys.system.mapper.RoleMapper;
import cn.ys.user.domain.Logininfo;
import cn.ys.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ys
 * @since 2022-12-26
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper mapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void settledIn(Shop shop) {
        // 判断两次密码是否一致
        if(!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new BussinessException("密码不一致");
        }

        // 判断店铺是否存在
        String shopName = shop.getName();
        Shop shopDb = shopMapper.findByName(shopName);
        if(shopDb != null){
            throw new BussinessException("店铺已存在");
        }

        // 判断店铺名是否违规
        Boolean textCensor = BaiduAuditUtils.TextCensor(shopName);
        if (!textCensor){
            throw new BussinessException("店铺名不合法");
        }

        // 判断店铺logo是否违规
        Boolean imgCensor = BaiduAuditUtils.ImgCensor(shop.getLogo());
        if (!imgCensor) {
            throw new BussinessException("logo不合法");
        }
        // 判断注册人是否重复注册
        String username = shop.getAdmin().getUsername();
        Employee employee = employeeMapper.findByName(username);
        if(employee != null && employee.getShop_id() != null){
            shopDb = shopMapper.findByAdminId(employee.getId());
            if(shopDb != null){
                throw new BussinessException("此人已经注册");
            }

        }
        // 所有都通过则添加店铺信息
        // 添加 shop
        shop.setRegisterTime(new Date());
        shop.setState(0);
        super.add(shop);
        // 添加 emp
        Employee emp = new Employee();
        Employee admin = shop.getAdmin();
        BeanUtils.copyProperties(admin,emp);
        emp.setAge(0);
        emp.setState(1);
        //  给emp 设置默认密码
        String salt = StrUtils.getComplexRandomString(32);
        emp.setSalt(salt);
        emp.setPassword(MD5Utils.encrypByMd5(salt+"1"));
        emp.setShop_id(shop.getId());
        //  同步 logininfo表数据
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(emp,logininfo);
        logininfo.setDisable(1);
        logininfo.setType(0);
        logininfoMapper.add(logininfo);
        //  更新emp的 logininfoid
        emp.setLogininfo_id(logininfo.getId());
        employeeMapper.add(emp);

        //  默认给当前人添加店长角色
        List<Role> roleList = roleMapper.findAll();
        Optional<Role> any = roleList.stream().filter(role -> "dz001".equals(role.getSn())).findAny();
        any.ifPresent(role->{
            roleMapper.addMiddle(emp.getId(), Collections.singletonList(role.getId()));
        });
        // 修改shop的adminId字段
        Shop updateShop = new Shop();
        updateShop.setId(shop.getId());
        updateShop.setAdminId(emp.getId());
        super.update(updateShop);
    }

    // 审核通过
    @Override
    public void auditPass(ShopAuditLogDto dto) {
        // 参数校验
        // 修改状态为已审核/审核通过/待激活
        Shop shop = new Shop();
        shop.setId(dto.getShopId());
        shop.setState(ShopEnum.AUDIT_PASS.getCode());
        shopMapper.update(shop);

        // 保存进日志
        ShopAuditLog shopAuditLog = new ShopAuditLog();
        shopAuditLog.setShopId(dto.getShopId());
        shopAuditLog.setNote(dto.getNote());
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(1L);
        mapper.add(shopAuditLog);

        // 发送邮件
        ShopEnterUtil.EnterAuditPass("1195548499@qq.com", dto.getShopId(), "1195548499@qq.com");
    }


    @Override
    public void reject(ShopAuditLogDto dto) {
        // 参数校验
        // 修改状态为已审核/审核通过/待激活
        Shop shop = new Shop();
        shop.setId(dto.getShopId());
        shop.setState(ShopEnum.AUDIT_NO_PASS.getCode());
        shopMapper.update(shop);

        // 保存进日志
        ShopAuditLog shopAuditLog = new ShopAuditLog();
        shopAuditLog.setShopId(dto.getShopId());
        shopAuditLog.setNote(dto.getNote());
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(1L);
        mapper.add(shopAuditLog);

        // 发送邮件
        ShopEnterUtil.EnterReject("1195548499@qq.com", dto.getNote(), dto.getShopId(), "1195548499@qq.com");
    }

    @Override
    public void jiHuo(Long id) {
        Shop shop = new Shop();
        shop.setState(ShopEnum.ACTIVITY.getCode());
        shop.setId(id);
        shopMapper.update(shop);
    }
}
